﻿<Window x:Class="PDOGenerator.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:pdogen="clr-namespace:PDOGenerator"
    xmlns:util="clr-namespace:PDOGenerator.Util"
    xmlns:pdoMap="clr-namespace:PDOGenerator.MapTypes"
    xmlns:views="clr-namespace:PDOGenerator.Views"
    Title="PDO Generator" MinHeight="200" MinWidth="600" Height="500" Width="800" Icon="/PDOGenerator;component/Icons/Cog.ico">

    <Window.Resources>
        <Style x:Key="Fader" TargetType="Grid">
            <Setter Property="Opacity" Value="{Binding Path=BackgroundFade}" />
        </Style>
    </Window.Resources>

    <Grid Style="{StaticResource Fader}" >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*" />
            <ColumnDefinition Width="5*" />
        </Grid.ColumnDefinitions>

        <Grid.Resources>
            <Style TargetType="Label">
                <Setter Property="Margin" Value="2,2,2,2" />
            </Style>
            <Style TargetType="TextBox">
                <Setter Property="Margin" Value="2,2,2,2" />
            </Style>
            <Style TargetType="TextBlock">
                <Setter Property="Margin" Value="2,2,2,2" />
            </Style>
            <Style TargetType="Button">
                <Setter Property="Margin" Value="2,2,2,2" />
            </Style>

            <util:VisiblityValueConverter x:Key="visibilityConverter" />




            <!--full width item, missing expander style 
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                <Setter Property="Padding" Value="1,0,0,0"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TreeViewItem}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition MinWidth="19" Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
                                <ToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"/>
                                <Border x:Name="Bd" SnapsToDevicePixels="true" Grid.Column="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" x:Name="PART_Header" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" ContentSource="Header"/>
                                </Border>
                                <ItemsPresenter x:Name="ItemsHost" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1"/>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsExpanded" Value="false">
                                    <Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>
                                </Trigger>
                                <Trigger Property="HasItems" Value="false">
                                    <Setter Property="Visibility" TargetName="Expander" Value="Hidden"/>
                                </Trigger>
                                <Trigger Property="IsSelected" Value="true">
                                    <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                                </Trigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsSelected" Value="true"/>
                                        <Condition Property="IsSelectionActive" Value="false"/>
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                                </MultiTrigger>
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
-->

            <!-- Full width item with XP style +/- expander buttons -->
            <!--
            <Style x:Key="TreeViewItemFocusVisual">
                <Setter Property="Control.Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Rectangle/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="ExpandCollapseToggleStyle" TargetType="{x:Type ToggleButton}">
                <Setter Property="Focusable" Value="False"/>
                <Setter Property="Width" Value="19"/>
                <Setter Property="Height" Value="13"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ToggleButton}">
                            <Border Width="19" Height="13" Background="Transparent">
                                <Border Width="9" Height="9" SnapsToDevicePixels="true" BorderBrush="#FF7898B5" BorderThickness="1" CornerRadius="1">
                                    <Border.Background>
                                        <LinearGradientBrush EndPoint="1,1" StartPoint="0,0">
                                            <GradientStop Color="White" Offset=".2"/>
                                            <GradientStop Color="#FFC0B7A6" Offset="1"/>
                                        </LinearGradientBrush>
                                    </Border.Background>
                                    <Path Margin="1,1,1,1" x:Name="ExpandPath" Fill="Black" Data="M 0 2 L 0 3 L 2 3 L 2 5 L 3 5 L 3 3 L 5 3 L 5 2 L 3 2 L 3 0 L 2 0 L 2 2 Z"/>
                                </Border>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsChecked" Value="True">
                                    <Setter Property="Data" TargetName="ExpandPath" Value="M 0 2 L 0 3 L 5 3 L 5 2 Z"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                <Setter Property="Padding" Value="1,0,0,0"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                <Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TreeViewItem}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition MinWidth="19" Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
                                <Border
        x:Name="Bd"
        Grid.ColumnSpan="3"
        SnapsToDevicePixels="true"
        Background="{TemplateBinding Background}"
        BorderBrush="{TemplateBinding BorderBrush}"
        BorderThickness="{TemplateBinding BorderThickness}"
        Padding="{TemplateBinding Padding}">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition MinWidth="19" Width="Auto"/>
                                            <ColumnDefinition/>
                                        </Grid.ColumnDefinitions>
                                        <ToggleButton
          x:Name="Expander"
          Style="{StaticResource ExpandCollapseToggleStyle}"
          IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"
          ClickMode="Press"/>
                                        <ContentPresenter
          Grid.Column="1"
          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
          x:Name="PART_Header"
          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
          ContentSource="Header"/>
                                    </Grid>
                                </Border>
                                <ItemsPresenter
        x:Name="ItemsHost"
        Grid.Column="1"
        Grid.ColumnSpan="2"
        Grid.Row="1"/>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsExpanded" Value="false">
                                    <Setter
         Property="Visibility"
         TargetName="ItemsHost"
         Value="Collapsed"/>
                                </Trigger>
                                <Trigger Property="HasItems" Value="false">
                                    <Setter
         Property="Visibility"
         TargetName="Expander"
         Value="Hidden"/>
                                </Trigger>
                                <Trigger Property="IsSelected" Value="true">
                                    <Setter
         Property="Background"
         TargetName="Bd"
         Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                    <Setter
         Property="Foreground"
         Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                                </Trigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsSelected" Value="true"/>
                                        <Condition Property="IsSelectionActive" Value="false"/>
                                    </MultiTrigger.Conditions>
                                    <Setter
         Property="Background"
         TargetName="Bd"
         Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                                    <Setter
         Property="Foreground"
         Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                                </MultiTrigger>
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter
         Property="Foreground"
         Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            
            
                            <Setter Property="HorizontalContentAlignment" Value="Center"/>

            
-->


            <Style TargetType="TreeViewItem" BasedOn="{StaticResource {x:Type TreeViewItem}}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TreeViewItem">
                            <StackPanel>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" MinWidth="19" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition />
                                    </Grid.RowDefinitions>
                                    <!--
             Note that the following do not work, but I believe the top 2 should?!
             <ToggleButton IsChecked="{TemplateBinding IsExpanded}" ClickMode="Press" Name="Expander">
             <ToggleButton IsChecked="{TemplateBinding Property=IsExpanded}" ClickMode="Press" Name="Expander">
             <ToggleButton IsChecked="{TemplateBinding Path=IsExpanded}" ClickMode="Press" Name="Expander">
             -->
                                    <ToggleButton IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press" Name="Expander">
                                        <ToggleButton.Style>
                                            <Style TargetType="ToggleButton">
                                                <Setter Property="UIElement.Focusable" Value="false" />
                                                <Setter Property="FrameworkElement.Width" Value="16" />
                                                <Setter Property="FrameworkElement.Height" Value="16" />
                                                <Setter Property="Control.Template">
                                                    <Setter.Value>
                                                        <ControlTemplate TargetType="ToggleButton">
                                                            <Border Padding="5,5,5,5" Background="#00FFFFFF" Width="16" Height="16">
                                                                <Path Fill="#00FFFFFF" Stroke="#FF989898" Name="ExpandPath">
                                                                    <Path.Data>
                                                                        <PathGeometry Figures="M0,0L0,6L6,0z" />
                                                                    </Path.Data>
                                                                    <Path.RenderTransform>
                                                                        <RotateTransform Angle="135" CenterX="3" CenterY="3" />
                                                                    </Path.RenderTransform>
                                                                </Path>
                                                            </Border>
                                                            <ControlTemplate.Triggers>
                                                                <Trigger Property="UIElement.IsMouseOver" Value="True">
                                                                    <Setter TargetName="ExpandPath" Property="Shape.Stroke" Value="#FF1BBBFA" />
                                                                    <Setter TargetName="ExpandPath" Property="Shape.Fill" Value="#00FFFFFF"/>
                                                                </Trigger>
                                                                <Trigger Property="ToggleButton.IsChecked" Value="True">
                                                                    <Setter TargetName="ExpandPath" Property="UIElement.RenderTransform">
                                                                        <Setter.Value>
                                                                            <RotateTransform Angle="180" CenterX="3" CenterY="3" />
                                                                        </Setter.Value>
                                                                    </Setter>
                                                                    <Setter TargetName="ExpandPath" Property="Shape.Fill" Value="#FF595959" />
                                                                    <Setter TargetName="ExpandPath" Property="Shape.Stroke" Value="#FF262626" />
                                                                </Trigger>
                                                            </ControlTemplate.Triggers>
                                                        </ControlTemplate>
                                                    </Setter.Value>
                                                </Setter>
                                            </Style>
                                        </ToggleButton.Style>
                                    </ToggleButton>
                                    <Border x:Name="Bd" 
                            HorizontalAlignment="Stretch" 
                            BorderThickness="{TemplateBinding Border.BorderThickness}" BorderBrush="{TemplateBinding Border.BorderBrush}"
                            Padding="{TemplateBinding Control.Padding}" 
                            Background="{TemplateBinding Panel.Background}" 
                            SnapsToDevicePixels="True" 
                            Grid.Column="1" >
                                        <ContentPresenter Content="{TemplateBinding HeaderedContentControl.Header}" ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}" ContentStringFormat="{TemplateBinding HeaderedItemsControl.HeaderStringFormat}" ContentSource="Header" Name="PART_Header" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                                    </Border>
                                    <ItemsPresenter Name="ItemsHost" Grid.Column="1" Grid.Row="1" />
                                </Grid>
                            </StackPanel>
                            <ControlTemplate.Triggers>
                                <Trigger Property="TreeViewItem.IsExpanded" Value="False">
                                    <Setter TargetName="ItemsHost" Property="UIElement.Visibility" Value="Collapsed" />
                                </Trigger>
                                <Trigger Property="ItemsControl.HasItems" Value="False">
                                    <Setter TargetName="Expander" Property="UIElement.Visibility" Value="Hidden" />
                                </Trigger>
                                <Trigger Property="TreeViewItem.IsSelected" Value="True" >
                                    <Setter TargetName="Bd" Property="Panel.Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                                    <Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                                </Trigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="TreeViewItem.IsSelected" Value="True" />
                                        <Condition Property="Selector.IsSelectionActive" Value="False" />
                                    </MultiTrigger.Conditions>
                                    <Setter TargetName="Bd" Property="Panel.Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
                                    <Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
                                </MultiTrigger>
                                <Trigger Property="UIElement.IsEnabled" Value="False">
                                    <Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>


        </Grid.Resources>

        <Menu Height="22" Name="appMenu" VerticalAlignment="Top" Grid.Row="0" Grid.ColumnSpan="2">
            <MenuItem Header="_File" >
                <MenuItem Name="openMenuItem" Header="_Open..." Click="openMenuItem_Click"/>
                <MenuItem Name="saveMenuItem" Header="_Save" Click="saveMenuItem_Click" />
                <MenuItem Name="saveAsMenuItem" Header="Save _As..." Click="saveAsMenuItem_Click" />
            </MenuItem>
            <MenuItem Header="_Action" >
                <MenuItem Name="readDatabaseMenuItem" Header="_Read Database" Command="{Binding ReadDatabase}" />
                <MenuItem Name="generateORMMenuItem" Header="_Generate..." Command="{Binding GenerateORMFiles}" />
            </MenuItem>
        </Menu>


        <Viewbox Width="500" Height="300" HorizontalAlignment="Center" Grid.ColumnSpan="2" Grid.RowSpan="2" 
                 VerticalAlignment="Center" Panel.ZIndex="999" 
                 Visibility="{Binding Path=IsBusy, Converter={StaticResource visibilityConverter}, ConverterParameter=Collapsed}" >
            <StackPanel Orientation="Vertical">
                <TextBlock Height="30" Text="{Binding Path=BusyMessage}" />
                <util:CircularProgressBar />
            </StackPanel>
        </Viewbox>

        <TreeView Grid.Row="1" Grid.Column="0" Name="projectTree" VerticalAlignment="Stretch" Background="#FFE5E5E5"
                  ItemsSource="{Binding Path=ProjectRoot}" >
            <TreeView.Resources>
                <HierarchicalDataTemplate ItemsSource="{Binding SchemaList}" DataType="{x:Type pdoMap:Project}">
                    <StackPanel Orientation="Horizontal"  >
                        <Image Height="25" Source="/PDOGenerator;component/Icons/Database.png" Stretch="Uniform"/>
                        <TextBlock Margin="5" Text="{Binding ProjectName}" />
                    </StackPanel>
                </HierarchicalDataTemplate>

                <HierarchicalDataTemplate ItemsSource="{Binding TableList}" DataType="{x:Type pdoMap:SchemaMap}" >
                    <StackPanel Orientation="Horizontal">
                        <Image Height="25" Source="/PDOGenerator;component/Icons/FilingCabinet.png" Stretch="Uniform"/>
                        <TextBlock Margin="5" Text="{Binding SchemaName}" />
                    </StackPanel>
                </HierarchicalDataTemplate>

                <HierarchicalDataTemplate ItemsSource="{Binding FullColumnList}" DataType="{x:Type pdoMap:TableMap}">
                    <StackPanel Orientation="Horizontal" >
                        <Image Height="25" Source="/PDOGenerator;component/Icons/InsertTable.png" Stretch="Uniform"/>
                        <TextBlock Margin="5" Text="{Binding TableName}" />
                    </StackPanel>
                </HierarchicalDataTemplate>

                <HierarchicalDataTemplate DataType="{x:Type pdoMap:ColumnMap}">
                    <StackPanel Orientation="Horizontal">
                        <Image Height="25" Source="{Binding ObjectPropertyImage}" Stretch="Uniform"/>
                        <TextBlock Margin="5" Text="{Binding ObjectPropertyName}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.Resources>


        </TreeView>

        <GridSplitter Width="4" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" Background="LightGray" />

        <StackPanel Grid.Row="1" Grid.Column="1" MinWidth="400" Margin="6,0,0,0" Background="White">
            <Grid>
                <ContentControl Content="{Binding ElementName=projectTree, Path=SelectedItem}">
                    <ContentControl.Resources>
                        <DataTemplate DataType="{x:Type pdoMap:Project}">
                            <ContentControl>
                                <views:ProjectView/>
                            </ContentControl>
                        </DataTemplate>

                        <DataTemplate DataType="{x:Type pdoMap:SchemaMap}">
                            <ContentControl>
                                <views:SchemaView/>
                            </ContentControl>
                        </DataTemplate>

                        <DataTemplate DataType="{x:Type pdoMap:TableMap}">
                            <ContentControl>
                                <views:TableView/>
                            </ContentControl>
                        </DataTemplate>

                        <DataTemplate DataType="{x:Type pdoMap:ColumnMap}">
                            <ContentControl>
                                <views:ColumnView/>
                            </ContentControl>
                        </DataTemplate>

                    </ContentControl.Resources>
                </ContentControl>
            </Grid>
        </StackPanel>

    </Grid>
</Window>
